package fw.qaz.asmx.reader.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;

import fw.qaz.asmx.reader.MainActivity;
import fw.qaz.asmx.reader.R;
import fw.qaz.asmx.reader.adapter.BookListAdapter;
import fw.qaz.asmx.reader.adapter.item.BookItem;
import fw.qaz.asmx.reader.utils.BookShelfUtil;

public class BookShelfFragment extends Fragment {
    private GridView gvBookShelf;

    private BookListAdapter bookListAdapter;

    public BookShelfFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_bookshelf, container, false);
        gvBookShelf = (GridView) v.findViewById(R.id.gv_book_shelf);//获取表格视图
        gvBookShelf.setEmptyView(v.findViewById(R.id.tv_empty_view));

        bookListAdapter = new BookListAdapter(container.getContext());//设置-

        bookListAdapter.setBooks(BookShelfUtil.getBooksFromJsonFile());//从文件中获取数据源
        gvBookShelf.setAdapter(bookListAdapter.getAdapter());     //      ---适配器

        //短按事件
        gvBookShelf.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {


                //复制出选中的书
                final BookItem bookSelected = new BookItem(
                        bookListAdapter.getBooks().get(i).getBookName(),
                        bookListAdapter.getBooks().get(i).getPath(),
                        bookListAdapter.getBooks().get(i).getReadProgress());

                ((MainActivity) getActivity()).startRead(bookSelected.getPath(), bookSelected.getReadProgress());
            }
        });
        //短按结束

        //长安事件
        gvBookShelf.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, final int i,
                                           long l) {
                //复制出选中的书
                final BookItem bookSelected = new BookItem(
                        bookListAdapter.getBooks().get(i).getBookName(),
                        bookListAdapter.getBooks().get(i).getPath(),
                        bookListAdapter.getBooks().get(i).getReadProgress());

                String[] bookMenus = new String[]
                        {
                                getResources().getString(R.string.book_menu_delete),//删除菜单
                                getResources().getString(R.string.book_menu_mao)//测试菜单
                        };

                new AlertDialog.Builder(getActivity())
                        .setItems(bookMenus, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int item) {
                                switch (item) {
                                    case 0://第一个菜单的事件
                                        BookShelfUtil
                                                .deleteBookFromJsonFile(bookSelected.getBookName(),
                                                        bookSelected.getPath());
                                        Toast.makeText(getActivity(), "删除成功", Toast.LENGTH_SHORT)
                                                .show();
                                        bookListAdapter.setBooks(
                                                BookShelfUtil.getBooksFromJsonFile());//重新获取数据源
                                        bookListAdapter.getAdapter().notifyDataSetChanged();//更新通知
                                        break;
                                    case 1://第2个菜单的事件
                                        Toast.makeText(getActivity(), "喵喵喵~~~", Toast.LENGTH_SHORT)
                                                .show();
                                        break;
                                }
                                dialog.dismiss();
                            }
                        }).show();
                return false;
            }
        });
        //长安事件结束
        return v;
    }

    public void refreshShelf() {
        bookListAdapter.setBooks(BookShelfUtil.getBooksFromJsonFile());//从文件中获取数据源
        bookListAdapter.getAdapter().notifyDataSetChanged();
    }
}
